package com.axx.android.utils;

import android.graphics.Bitmap;
import android.os.Environment;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.text.TextUtils;

import com.axx.android.LetterApp;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.BitmapCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/21 0021.
 */

public class HttpUtils {
    public static void getBitmap(String uri, final int size, final SJBitmapCallBack callback) {
        OkGo.<Bitmap>get(uri).execute(new BitmapCallback() {
            @Override
            public void onSuccess(Response<Bitmap> response) {
                Bitmap bitmap = response.body();
                bitmap = Bitmap.createScaledBitmap(bitmap, ViewUtils.dip2px(OkGo.getInstance().getContext(), size), ViewUtils.dip2px(OkGo.getInstance().getContext(), size), false);

                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(OkGo.getInstance().getContext().getResources(), bitmap);
                circularBitmapDrawable.setCircular(true);
                circularBitmapDrawable.setAntiAlias(true);

                callback.onSuccess(ImageUtils.drawableToBitmap(circularBitmapDrawable));

            }
        });
    }
    public static void getBitmapV2(String uri, final int size, final SJBitmapCallBack callback) {
        int realSize = ViewUtils.dip2px(OkGo.getInstance().getContext(), size);
        Glide.with(OkGo.getInstance().getContext()).load(uri).override(realSize,realSize).into(new SimpleTarget<GlideDrawable>() {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                //drawable转bitmap  bitmap转圆角drawable  圆角drawable转bitmap
                Bitmap bitmap =  ImageUtils.drawableToBitmap(resource);
                //bitmap = Bitmap.createScaledBitmap(bitmap, ViewUtils.dip2px(OkGo.getInstance().getContext(), 32), ViewUtils.dip2px(OkGo.getInstance().getContext(), size), false);

                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(OkGo.getInstance().getContext().getResources(), bitmap);
                circularBitmapDrawable.setCircular(true);
                circularBitmapDrawable.setAntiAlias(true);
                bitmap =  ImageUtils.drawableToBitmap(resource);
                callback.onSuccess(bitmap);
            }
        });
    }

    public static void postFile(int imageSize,String url, String fileName, File file, Map<String, String> data, final SJCallBack sjCallBack) {
        byte[] images = ImageUtils.compressBitmap(file.getPath(),imageSize);
        file = saveFile(images);
        OkGo.<String>post(url)
                .isMultipart(true)
                .params(fileName, file)
                .params(data, false)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        sjCallBack.onSuccess(response.body());
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            String token = jsonObject.getString("token");
                            if (!TextUtils.isEmpty(token) && !"null".equals(token)) {
                                if (LetterApp.authUser != null) {
                                    LetterApp.authUser.setToken(token);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        sjCallBack.onError();
                    }
                });
    }

    private static File saveFile(byte[] images) {
        File dir = new File(Environment.getExternalStorageDirectory(), "/iletter/image");
        File image = null;
        try {
            if (!dir.exists()) {
                dir.mkdirs();
            }

            long time = System.currentTimeMillis();
            image = new File(dir, "/" + time + ".png");
            if (!image.exists()) {
                image.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(image);
            fileOutputStream.write(images);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    public static void post(String url, Map<String, String> data, final SJCallBack sjCallBack) {
        OkGo.<String>post(url).params(data, false).execute(new StringCallback() {

            @Override
            public void onSuccess(Response<String> response) {
                sjCallBack.onSuccess(response.body());
                try {
                    JSONObject jsonObject = new JSONObject(response.body());
                    String token = jsonObject.getString("token");
                    if (!TextUtils.isEmpty(token) && !"null".equals(token)) {
                        if (LetterApp.authUser != null) {
                            LetterApp.authUser.setToken(token);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                sjCallBack.onError();
            }
        });
    }

    public interface SJCallBack {
        void onSuccess(String result);

        void onError();
    }

    public interface SJBitmapCallBack {
        void onSuccess(Bitmap bitmap);
    }
}
